﻿#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using System.Collections.Concurrent;
using UnityEditor;

namespace BF
{
    public static class EditorLoom
    {
        private static readonly object _lock = new object();
        private static readonly List<Action> _actions = new List<Action>();
        private static readonly List<Action> _temp = new List<Action>();

        public static void QueueOnMainThread(Action action)
        {
            _actions.Add(action);
        }

        static EditorLoom()
        {
            EditorApplication.update += Update;
        }

        private static void Update()
        {
            lock (_lock)
            {
                _temp.Clear();
                _temp.AddRange(_actions);
                _actions.Clear();
            }
            foreach (var action in _temp)
            {
                action?.Invoke();
            }
        }
    }
}
#endif